package Service;

import GUITOOl.*;

import GUI.ChatFrame;
import Utils.ImageIconUtils;
import Utils.IsImage;

import javax.swing.*;
import java.awt.*;
import java.io.*;

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

/**
 * The type Show chat history.
 *
 * @author dzr, wyc, zry
 * @version 1.0
 */
public class ShowChatHistory {

    /**
     * Show chatHistory into file.
     *
     * @param s_uid             the sender's uid
     * @param s_name           the sender's name
     * @param r_uid             the receiver's uid
     * @param r_name            the receiver's name
     * @param parent            the parent frame
     * @param friendChatHistory the file to save friend's chatHistory
     * @throws IOException the io exception
     */
    public static void showChatHistory(int s_uid, String s_name, int r_uid, String r_name, ChatFrame parent, File friendChatHistory) throws IOException {
        MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
        Color bgColor = new Color(255, 255, 255);
        MyLineBorder myLineBorder1 = new MyLineBorder(bgColor, 1 , true);
        MyLineBorder myLineBorder2 = new MyLineBorder(new Color(39, 121, 181),1,true);

        JDialog dialog = new JDialog(parent, "与" + r_name + "的聊天记录");
        Image frameImage= ImageIconUtils.getRawPicture("picture.png").getImage();
        dialog.setIconImage(frameImage);
        dialog.setBackground(bgColor);

        JTextArea jTextArea = new JTextArea(40,30);
        jTextArea.setBorder(myLineBorder);
        jTextArea.setEditable(false);
        jTextArea.setLineWrap(true);

        Reader reader = new FileReader(friendChatHistory);
        BufferedReader br = new BufferedReader(reader);
        String line;

        JPanel msg_his=new JPanel();
        msg_his.setBorder(myLineBorder);
        msg_his.setBounds(555,555,400,600);
        msg_his.setBackground(bgColor);
        msg_his.setLayout(new BoxLayout(msg_his,BoxLayout.Y_AXIS));
        while((line = br.readLine()) != null){
            int uid = Integer.parseInt(line.substring(line.length() - 6, line.length() - 1));
            JPanel panel = new JPanel();
            if(uid == s_uid){
                panel=new JPanel(new FlowLayout(FlowLayout.TRAILING));
            }
            else {
                panel=new JPanel(new FlowLayout(FlowLayout.LEADING));
            }
            panel.setBorder(myLineBorder2);
            MyTextArea myTextArea = new MyTextArea(line);

            line = br.readLine();
            File file = new File(line);
            if(file.exists()){
                if (IsImage.isImage(file)){
                    panel.add(new MyImageLabel(line, dialog));
                }else{
                    panel.add(new MyFileLable(line));
                }
            }else {
                myTextArea.append("\n    ");
                myTextArea.append(line);
            }
            panel.add(myTextArea);
            panel.setBackground(bgColor);
            msg_his.add(panel);
            msg_his.setBackground(bgColor);
        }

        JScrollPane jp=new JScrollPane(msg_his);
        jp.setBorder(null);
        jp.getVerticalScrollBar().setUI(new MyScrollBarUI());
        jp.getHorizontalScrollBar().setUI(new MyScrollBarUI());
        jp.setBounds(566, 555, 400, 800);
        jp.setBackground(bgColor);

        dialog.setBounds(100, 100, 400, 800);
        dialog.add(jp);
        dialog.setBackground(bgColor);
        dialog.setVisible(true);
        dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        br.close();
        reader.close();
    }
}
